package org.snail.attendence.service.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.log4j.Log4j;
import org.snail.attendence.bean.AliWorkDTO;
import org.snail.attendence.service.impl.AttendenceMongoOptionService;
import org.snail.constant.AttendenceConstant;
import org.snail.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.text.ParseException;
import java.util.List;

@Log4j
@Component("attendenceMongoListener")
public class AttendenceMongoListener implements MessageListener {

    @Autowired
    private AttendenceMongoOptionService attendenceMongoOption;

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        String text = null;
        try {
            text = textMessage.getText();
            List<AliWorkDTO> aliWorkDTOList = JSON.parseObject(text, new TypeReference<List<AliWorkDTO>>(){});
            log.error("=======================进行activemq  attendenceMongo存储操作,消息内容：" + aliWorkDTOList);
            attendenceMongoOption.saveAttendenceList(aliWorkDTOList, AttendenceConstant.ATTENDENCE_MONGO);
            log.error("存储考勤信息 MongoDB 结束");
        } catch (JMSException e) {
            log.error("考勤消息队列异常:"+e.getMessage());
        } catch (CommonException e) {
            log.error("考勤消息队CommonException:"+e.getMessage());
        }catch (Exception e){
            log.error("保存考勤至mongoDB未知错误:"+e.getMessage());
        }
    }
}